package com.lh.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.LeadLeader;
import com.lh.entity.table.ProblemFeedback;
import com.lh.mapper.ProblemFeedbackMapper;
import com.lh.mapper.ProcessLinkRecordMapper;
import com.lh.service.ProblemFeedbackService;
import com.lh.utils.Answer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProblemFeedbackServiceImpl implements ProblemFeedbackService {

    @Resource
    private ProblemFeedbackMapper problemFeedbackMapper;

    @Override
    public Answer<Object> getProblemFeedbackPage(JSONObject reqJson) {
        Integer pageSize = reqJson.getInteger("pageSize"); // 默认10条/页
        Integer pageNum = reqJson.getInteger("pageNum");    // 默认第1页
        if (pageNum < 1) pageNum = 1; // 防止pageNum为0或负数
        Page<ProblemFeedback> queryPage = new Page<>(pageNum, pageSize);
        String name = reqJson.getString("name");
        String department = reqJson.getString("department");
        String feedbackItem = reqJson.getString("feedbackItem");
        LambdaQueryWrapper<ProblemFeedback> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            queryWrapper.eq(ProblemFeedback::getName,name);
        }
        if (StringUtils.isNotBlank(department)){
            queryWrapper.eq(ProblemFeedback::getDepartment,department);
        }
        if (StringUtils.isNotBlank(feedbackItem)){
            queryWrapper.like(ProblemFeedback::getFeedbackItem,feedbackItem);
        }
        queryWrapper.orderByDesc(ProblemFeedback::getFeedbackTime);
        Page<ProblemFeedback> problemFeedbackPage = problemFeedbackMapper.selectPage(queryPage, queryWrapper);
        return Answer.ok(problemFeedbackPage);
    }

    @Override
    public Answer<Object> addProblemFeedback(ProblemFeedback problemFeedback) {
        problemFeedback.setFeedbackTime(new Date());
        problemFeedbackMapper.insert(problemFeedback);
        return Answer.ok("添加成功");
    }

    @Override
    public Answer<Object> updateProblemFeedbackById(ProblemFeedback problemFeedback) {
        problemFeedbackMapper.updateById(problemFeedback);
        return Answer.ok("修改成功");
    }

    @Override
    public Answer<Object> deleteProblemFeedbackById(JSONObject reqJson) {
        Integer id = reqJson.getInteger("id");
        problemFeedbackMapper.deleteById(id);
        return Answer.ok("删除成功");
    }

    @Override
    public Answer<Object> getProblemFeedbackByName(JSONObject reqJson) {
        String name = reqJson.getString("name");
        List<ProblemFeedback> problemFeedbacks = problemFeedbackMapper.selectList(new LambdaQueryWrapper<ProblemFeedback>().like(ProblemFeedback::getName, name));
        return Answer.ok(problemFeedbacks);
    }
}
